<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once "objetos/Email.php";
require_once "objetos/Usuario.php";
require_once "server/EmailServer.php";

function listarEmailsUsuario($userId) {
    $emailServer = new EmailServer();

    $conn = $emailServer->imapOpen($userId->getLogin(), $userId->getSenha());
    $list = $emailServer->listEmailsImap($conn, $userId->getLogin());
    $emails = array();
    if ($list != false) {
        foreach ($list as $numEmail) {
            $emailImap = $emailServer->obterInfoEmail($conn, $numEmail);
            $email = new Email();
            $email->setId($numEmail);
            $email->setAssunto($emailImap[0]->subject);
            $from = explode('"',$emailImap[0]->from);
            $email->setRemetente($from[1]);
            $time = strtotime( $emailImap[0]->date );
            $email->setDataRecebido(date( 'd/m/Y H:i:s', $time ));
            $email->setStatus($emailImap[0]->seen);
            $emails[] = $email;
        }
    }
        
    $emailServer->imapClose($conn);

    return $emails;
}

function listarLixeiraEmailsUsuario($userId) {
    $emailServer = new EmailServer();

    $conn = $emailServer->imapOpen($userId->getLogin(), $userId->getSenha());
    $list = $emailServer->listDeletedEmailsImap($conn, $userId->getLogin());
    $emails = array();
    if ($list != false) {
        foreach ($list as $numEmail) {
            $emailImap = $emailServer->obterInfoEmail($conn, $numEmail);
            $email = new Email();
            $email->setId($numEmail);
            $email->setAssunto($emailImap[0]->subject);
            $from = explode('"',$emailImap[0]->from);
            $email->setRemetente($from[1]);
            $time = strtotime( $emailImap[0]->date );
            $email->setDataRecebido(date( 'd/m/Y H:i:s', $time ));
            $email->setStatus($emailImap[0]->seen);
            $emails[] = $email;
        }
    }
        
    $emailServer->imapClose($conn);

    return $emails;
}

function listarEmailsEnviadosUsuario($userId) {
    $emailServer = new EmailServer();

    $conn = $emailServer->imapOpenOutBox($userId->getLogin(), $userId->getSenha());
    $list = $emailServer->listSendedEmailsImap($conn, $userId->getLogin());
    $emails = array();
    if ($list != false) {
        foreach ($list as $numEmail) {
            $emailImap = $emailServer->obterInfoEmail($conn, $numEmail);
            $emailHeader = imap_headerinfo($conn, $numEmail);
            $email = new Email();
            $email->setId($numEmail);
            $email->setAssunto($emailImap[0]->subject);
            $to = explode('"',$emailImap[0]->to);
            $email->setDestinatario($to[1]);
            $time = strtotime( $emailHeader->MailDate );
            $email->setDataEnviado(date( 'd/m/Y H:i:s', $time ));
            $email->setStatus($emailImap[0]->seen);
            $emails[] = $email;
        }
    }
        
    $emailServer->imapClose($conn);

    return $emails;
}

function listarRascunhos($userId) {
    $emailServer = new EmailServer();

    $conn = $emailServer->imapOpenRascunho($userId->getLogin(), $userId->getSenha());
    $list = $emailServer->listEmailsImap($conn, $userId->getLogin());
    $emails = array();
    if ($list != false) {
        foreach ($list as $numEmail) {
            $emailImap = $emailServer->obterInfoEmail($conn, $numEmail);
            $email = new Email();
            $email->setId($numEmail);
            $email->setAssunto($emailImap[0]->subject);
            $from = explode('"',$emailImap[0]->from);
            $email->setRemetente($from[1]);
            $to = explode('"',$emailImap[0]->to);
            $email->setDestinatario($to[1]);
            $time = strtotime( $emailImap[0]->date );
            $email->setDataRecebido(date( 'd/m/Y H:i:s', $time ));
            $email->setStatus($emailImap[0]->seen);
            $emails[] = $email;
        }
    }
        
    $emailServer->imapClose($conn);

    return $emails;
}

?>
